package com.allqj.sfx.ddd.member.infrastructure.repository;

import com.allqj.sfx.ddd.member.domain.entity.flow.FlowDef;
import com.allqj.sfx.ddd.member.domain.repository.FlowDefRepository;
import com.allqj.sfx.ddd.member.infrastructure.converter.FlowDefConverter;
import com.allqj.sfx.ddd.member.infrastructure.dao.FlowDefDAO;
import com.allqj.sfx.ddd.member.infrastructure.po.FlowDefDO;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * @author sjw
 * @className FlowTypeRepositoryImpl
 * @date 2022/7/22 16:31
 * @description: FlowDefRepository 具体实现
 * Repository接口定义位于domain层
 * Repository接口具体实现位于infrastructure层
 * 与底层db解耦
 */
@Repository
public class FlowDefRepositoryImpl implements FlowDefRepository {

    private final FlowDefDAO dao;

    public FlowDefRepositoryImpl(FlowDefDAO dao) {
        this.dao = dao;
    }

    @Override
    public FlowDef save(FlowDef flowDef) {
        if (flowDef == null) return null;
        FlowDefDO flowDefDO = FlowDefConverter.INSTANCE.entity2Do(flowDef);
        flowDefDO = dao.save(flowDefDO);
        return FlowDefConverter.INSTANCE.do2Entity(flowDefDO);
    }

    @Override
    public FlowDef find(Integer id) {
        if (id == null) return null;
        Optional<FlowDefDO> flowDefDOOptional = dao.findById(id);
        return flowDefDOOptional.map(flowDefDO -> FlowDefConverter.INSTANCE.do2Entity(flowDefDO)).orElse(null);
    }

    @Override
    public FlowDef functionFlowDef(Integer moduleId, Integer functionId, Integer status) {
        FlowDefDO flowDefDO = dao.findByModuleIdAndFunctionIdAndStatusAndIsdeleteFalse(moduleId, functionId, status);
        return FlowDefConverter.INSTANCE.do2Entity(flowDefDO);
    }
}
